The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
In [53]:
// to find the largest prime factor `N` we'll find all primes up
// to sqrt(N), filter out non factors and find the largest
let limit = 600851475143UL
let compositesInRange fromNum toNum =
[|2 .. toNum/fromNum|]
|> Array.map (fun x -> fromNum * x)
let primesTo n =
// start off assuming every number from 0 .. n is prime
let isPrime = Array.init (1+n) (fun _ -> true)
// simple sieve of eratosthenes - update "primeness" of
// each number in the isPrime array
for x in 2 .. int(sqrt(double(n))) do
if isPrime.[x] then
compositesInRange x n
|> Array.map (fun i -> isPrime.[i] <- false)
|> ignore
// now return numbers which are "true" in isPrime array
[2..n]
|> List.filter (fun x -> isPrime.[x])
primesTo (int(sqrt(double(limit))))
|> List.filter (fun x -> (limit % uint64(x) = 0UL))
|> List.last
Out[53]:
In [ ]: